package com.share.netdisk.user.controller;


import com.share.netdisk.user.model.User;
import com.share.netdisk.user.service.UserService;
import io.swagger.v3.oas.annotations.Operation;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RequestMapping("/user")
@Slf4j
@RestController
public class UserController {

    @Autowired
    private UserService userService;

    //登录
    @PostMapping("/login")
    @Operation(summary = "用户登录", description = "用户登录")
    public String login(@RequestBody @Valid User user){
        log.info("user:{}",user);
        return userService.login(user);
    }
    //注册
    @PostMapping("/register")
    @Operation(summary = "用户注册", description = "用户注册")
    public String register(@RequestBody @Valid User user){
        log.info("user:{}",user);
        return userService.register(user);
    }

    //根据用户名判断用户是否存在
    @GetMapping("/checkUserName")
    @Operation(summary = "根据用户名判断用户是否存在", description = "根据用户名判断用户是否存在")
    public boolean checkUserName(@RequestParam("username") String username){
        log.info("username:{}",username);
        return userService.isExist(username);
    }

    //获取验证码
    @GetMapping("/getCaptcha")
    @Operation(summary = "获取验证码", description = "获取验证码")
    public ResponseEntity<byte[]> getCaptcha(){
        return userService.generateCaptcha();
    }

    //验证验证码
    @PostMapping("/validate")
    public boolean validateCaptcha(@RequestParam("uuid") String uuid, @RequestParam("code") String code) {
        return userService.validateCaptcha(uuid, code);
    }

    //注销
    @GetMapping("/logout")
    @Operation(summary = "用户注销", description = "用户注销")
    public Boolean logout(@RequestParam String username){
        log.info("username:{}",username);
        return userService.logout(username);
    }

}
